<template>
  <div class="baseinfo" v-title data-title="新产品质量评估立项界面">
    <a-form-model ref="ruleForm" :model="form" :rules="rules" :label-col="labelCol" :wrapper-col="wrapperCol">
      <a-row :gutter="24">
        <a-col :span="8">
          <a-form-model-item label="项目编号" prop="projectNo" :label-col="{ span: 8 }" :wrapper-col="{ span: 15 }">
            {{form.projectNo}}
          </a-form-model-item>
        </a-col>
        <a-col :span="8">
          <a-form-model-item
            ref="projectName"
            label="项目名称"
            prop="projectName"
            :label-col="{ span: 8 }"
            :wrapper-col="{ span: 15 }"
          >
            <a-input
              v-model="form.projectName"
              @blur="
                () => {
                  $refs.projectName.onFieldBlur()
                }
              "
              placeholder="请输入有效的项目名称" 
              :disabled="form.status>1" 
            />
          </a-form-model-item>
        </a-col>
        <a-col :span="8">
          <a-form-model-item
            label="项目分类"
            prop="projectTypeSubCategory"
            :label-col="{ span: 8 }"
            :wrapper-col="{ span: 15 }"
          >
            <!-- <a-select
              v-model="form.projectTypeSubCategory"
              placeholder="请选择项目分类"
              :options="projectCategoryOptions" :disabled="form.id!=''"  
            >
              <a-icon slot="suffixIcon" type="plus" />
            </a-select> -->
            <span style="color: #f7a961; margin: 0 6px; font-size: 16px">{{ subName }}</span>
          </a-form-model-item>
        </a-col>
      </a-row>
      
      <a-row :gutter="24">
        <a-col :span="8">
          <a-form-model-item
            ref="mainDeptName"
            label="主导经营体"
            prop="mainDeptName"
            :label-col="{ span: 8 }"
            :wrapper-col="{ span: 15 }"
          >
            <a-input v-model="form.mainDeptName" placeholder="请填写正确的组织架构名称，系统自动校验" />
          </a-form-model-item>
        </a-col>
        <a-col :span="8">
          <a-form-model-item
            label="新产品类型"
            prop="newProductType"
            :label-col="{ span: 8 }"
            :wrapper-col="{ span: 15 }"
          >
            <a-select
              v-model="form.newProductType"
              placeholder="请选择新产品类型"
              :options="newProductTypeOptions"
              @change="newProductTypeChange"
            >
              <a-icon slot="suffixIcon" type="plus" />
            </a-select>
          </a-form-model-item>
        </a-col>
        <a-col :span="8">
            <a-form-model-item
              ref="newProductListingPlan"
              label="新产品计划上市时间"
              prop="newProductListingPlan"
              :label-col="{ span: 8 }"
              :wrapper-col="{ span: 15 }"
            >
              <a-date-picker  v-model="form.newProductListingPlan"  placeholder="请输入新产品计划上市时间" style="width:100%"/>
            </a-form-model-item>
          </a-col>
      </a-row>
       <a-row :gutter="24">
         <a-col :span="8">
          <a-form-model-item
            ref="tcProjectRealName"
            label="TC项目名称"
            prop="tcProjectRealName" 
            :label-col="{ span: 8 }"
            :wrapper-col="{ span: 15 }"
          >
            <a-input
              v-model="form.tcProjectRealName"
              @blur="
                () => {
                  $refs.tcProjectRealName.onFieldBlur()
                }
              "
              placeholder="如有则复制tc的项目名称来填入"   
            />
          </a-form-model-item>
        </a-col>
       </a-row>
      <a-row :gutter="24">
        <a-col :span="24">
          <a-form-model-item
            ref="newProductNameAndSpec"
            prop="newProductNameAndSpec"
            :label-col="{ span: 3 }"
            :wrapper-col="{ span: 21 }"
            label="新产品名称及规格"
          >
            <a-textarea
              v-model="form.newProductNameAndSpec"
              @blur="
                () => {
                  $refs.newProductNameAndSpec.onFieldBlur()
                }
              "
              :auto-size="{ minRows: 3, maxRows: 5 }"
              style="width: 100%"
              placeholder="请填写清楚本次新产品的所有的规格，填写标准按照产品名称+规格，例如0添加金标生抽500ml；0添加金标生抽1.9L。"
            />
          </a-form-model-item>
        </a-col>
      </a-row>
      <a-row :gutter="24">
        <a-col :span="24">
          <a-form-model-item
            ref="evaluationObject"
            prop="evaluationObject"
            :label-col="{ span: 3 }"
            :wrapper-col="{ span: 21 }"
            label="评估对象(产品)"
          >
            <a-textarea
              v-model="form.evaluationObject"
              @blur="
                () => {
                  $refs.evaluationObject.onFieldBlur()
                }
              "
              :auto-size="{ minRows: 3, maxRows: 5 }"
              style="width: 100%"
              placeholder="填写本次新产品质量评估的具体产品，填写标准按照产品名称+规格，例如0添加金标生抽500ml。"
            />
          </a-form-model-item>
        </a-col>
      </a-row>

      <a-row :gutter="24">
        <a-col :span="24">
          <a-form-model-item
            ref="newProductKeyword"
            label="关键字"
            prop="newProductKeyword"
            :label-col="{ span: 3 }"
            :wrapper-col="{ span: 21 }"
          >
            <a-textarea
              v-model="form.newProductKeyword"
              @blur="
                () => {
                  $refs.newProductKeyword.onFieldBlur()
                }
              "
              :auto-size="{ minRows: 3, maxRows: 5 }"
              style="width: 100%"
              placeholder="请填写至少3组关键词，关键词之间用中文【；】隔开。"
            />
          </a-form-model-item>
        </a-col>
      </a-row>
      <five-to-five ref="fiveForm" :projectBuildId="projectBuildId" :change-id="changeId" @addField="addFiveDataToForm"/>
    </a-form-model>
  </div>
</template>
<script>
import $ from 'jquery'
import { getAction, getActionAsync, postAction, postActionAsync } from '@/api/manage'
import { getParamValFromUrl, strNotNull } from '@/utils/common.js'
import Vue from 'vue'
export default {
  components: {
    FiveToFive: () => import('@/components/tools/projectBuildStup/baseInfo/FiveToFive')
  },
  props: {
    projectBuildId: {
      Type: String,
      default: '',
    },
    changeId: {
      Type: String,
      default: '',
    },
    selProjectType: {
      Type: String,
      default: '',
    }
  },
  data() {
    return {
      labelCol: { span: 8 },
      wrapperCol: { span: 15 },
      form: {
        id:'',
        projectClass: '20',
        projectNo: '',
        projectName: '',
        mainDeptName: '',
        mainDeptId: '',
        projectTypeSubCategory: '',
        newProductType: '',
        newProductTypeName: '',
        newProductNameAndSpec: '',
        evaluationObject: '',
        newProductKeyword: '', 
        newProductListingPlan: '', 
        status: 0 ,
        tcProjectRealName:''
      },
      rules: {
        projectName: [
          { required: true, message: '请输入有效的项目名', trigger: 'blur' },
          { min: 1, max: 50, message: '项目名不超过50个字符', trigger: 'blur' },
        ],
        tcProjectRealName: [
          { min: 1, max: 50, message: 'TC项目名称不超过50个字符', trigger: 'blur' }
        ],
        mainDeptName: [
          { required: true, message: '请填写正确的组织架构名称，系统自动校验', trigger: 'blur' }
        ],
        projectTypeSubCategory: [{ required: true, message: '请选择项目分类', trigger: 'change' }],
        newProductType: [{ required: true, message: '请选择新产品类型', trigger: 'change' }],
        newProductListingPlan: [{ required: true, message: '请选择新产品计划上市时间', trigger: 'change' }],
        newProductNameAndSpec: [
          { required: true, message: '请输入新产品名称及规格', trigger: 'blur' },
          { min: 1, max: 500, message: '新产品名称及规格不超过500个字符', trigger: 'blur' },
        ],
        evaluationObject: [
          { required: true, message: '请输入评估对象（产品）', trigger: 'blur' },
          { min: 1, max: 500, message: '评估对象（产品）不超过500个字符', trigger: 'blur' },
        ],
        newProductKeyword: [
          { required: true, message: '请填写至少3组关键词，关键词之间用中文【；】隔开', trigger: 'blur' },
          { min: 1, max: 500, message: '关键字不超过500个字符', trigger: 'blur' },
        ]
      },
      projectCategoryOptions: [],
      newProductTypeOptions: [],
      url: {
        codeInfoList: '/manage/codeInfo/list',
        getProjectByNo: '/projectBuild/getProjectByNo',
        save: '/projectBuild/add',
        queryVOById: '/projectBuild/queryVOById',
      },
      loadFinish: false,
      subName: ''
    }
  },
  computed: {},
  methods: {
    addFiveDataToForm(val,valRef) {
      this.$set(this.form, valRef, val)
    },
    async doSubmit() {
      if(!this.loadFinish){
        layer.message.warning('请等待页面加载完毕')
        return {}  
      }
      var validR=false;
      try{
        validR = await this.$refs.ruleForm.validate();
        validR = await this.$refs.fiveForm.checkFiveWriteStatus()
      }catch(e){
        validR = false;
      }
      console.log('validR',validR);
      this.form.validResult = validR;
      let res=await this.doSave();
        this.projectBuildId=res;
      return {valid:validR,id:res};
    },
    async doSave() {
      let flag=null;
      var keywords = this.form.newProductKeyword
      if (strNotNull(keywords)) {
        let keywordLen = keywords.split('；').filter((x) => !!x).length
        if (keywordLen < 3) {
          this.$message.warning('请填写至少3组关键词，关键词之间用中文【；】隔开');
          return flag;
        }
      }

      this.form.changeId=this.changeId;
      var formData = JSON.parse(JSON.stringify(this.form));

      var vm = this
      await postActionAsync(this.url.save, formData)
        .then((res) => {
          if (res.code == 200) {
            vm.form.id = res.result.id;
            vm.form.projectNo = res.result.projectNo;
            this.$message.success('保存成功')
            return flag=res.result.id;
          } else {
            this.$message.warning('保存数据出错:' + res.message)
          }
        })
        .catch((res) => {
          this.$message.warning('保存数据出错:' + res.message)
        })
        return flag;
    },
    resetForm() {
      this.$refs.ruleForm.resetFields()
    },
    //初始化项目子类
    initProjectCategoryOptions() {
      var vm = this
      getAction(this.url.codeInfoList, { type: '项目子类', isdel: 0, parentid: vm.form.projectClass,pageSize:-1 })
        .then((res) => {
          if (res.code == 200) {
            var list = res.result
            if (list != null) {
              list.records.map((item) => {
                vm.projectCategoryOptions.push({ value: item.code, label: item.name })
              })

              if (vm.projectCategoryOptions.length === 1) {
                vm.form.projectTypeSubCategory = vm.projectCategoryOptions[0].value
              }
            }
          } else {
            this.$message.warning('查询项目子类失败:' + res.message)
          }
        })
        .catch((res) => {
          this.$message.warning('查询项目子类失败:' + res.message)
        })
    },
    initSelectOptions() {
      var vm = this
      getAction(this.url.codeInfoList, { type: '新质量评估新产品类型', pageNo:1,pageSize:9999,isdel: 0 })
        .then((res) => {
          if (res.code == 200) {
            var list = res.result
            if (list != null) {
              list.records.map((item) => {
                vm.newProductTypeOptions.push({ value: item.code, label: item.name })
              })
            }
          } else {
            this.$message.warning('查询产品粘贴商标失败:' + res.message)
          }
        })
        .catch((res) => {
          this.$message.warning('查询产品粘贴商标失败:' + res.message)
        })
    },
    newProductTypeChange (value,option) {
      this.form.newProductTypeName = option.componentOptions.children[0].text;
    },
    //如果有id，则加载已填写信息
    initData() {
      if (this.projectBuildId != null && this.projectBuildId != '') {
        var vm = this
        getAction(this.url.queryVOById, { id: this.projectBuildId,changeId: this.changeId })
          .then((res) => {
            if (res.code == 200) {
              var result = res.result
              if (result != null) {
                this.form = result
                
                this.initSubName()
                this.loadFinish = true
              } else {
                vm.$message.warning('初始化项目信息为空')
              }
            } else {
              this.$message.warning('初始化项目信息失败:' + res.message)
            }
          })
          .catch((res) => {
            this.$message.warning('初始化项目信息失败:' + res.message)
          })
      }else{
        this.loadFinish = true
      }
    },
    initSubName () {
      var vm = this
      getAction(this.url.codeInfoList, { type: '项目子类', isdel: 0,parentid:vm.form.projectClass,code:vm.form.projectType,pageSize:-1})
        .then((res) => {
          if (res.code == 200) {
            var list = res.result
            if (list != null) {
              this.subName = list.records[0].name
            }
          } else {
            this.$message.warning('项目子类:' + res.message)
          }
        })
        .catch((res) => {
          this.$message.warning('项目子类:' + res.message)
        })
    },
  },
  mounted() {
    if (this.projectBuildId == null || this.projectBuildId == '') {
        this.form.projectType = this.selProjectType
        this.form.projectTypeSubCategory = this.selProjectType
        this.form.tcProjectRealName = this.$route.query.tcname
        this.initSubName()
     }

    this.initProjectCategoryOptions()
    this.initSelectOptions()
    this.initData()
  },
}
</script>
<style lang="less" scoped>
/deep/ .ant-btn-primary {
  width: 85px;
  height: 30px;
  border-radius: 2px;
  background: #4095e4;
  margin-right: 10px;
}
/deep/ .default {
  width: 85px;
  height: 30px;
  border-radius: 2px;
}
.baseinfo {
  background-color: #fff;
  border: 1px solid #eaeaea;
  padding: 30px;
  .btn {
    display: flex;
    justify-content: center;
  }
}
</style>